﻿/**
 * Proyecto: Cognitum
 * Global IT Services 2008
 * @author Mislas
 */
Cognitum.Polizas.General.Entity = function(){
    if (LOG.isDebugEnabled()) {
        LOG.debug('[General.Entity.ctor] <-');
    }
    Cognitum.Polizas.General.Entity.superclass.constructor.call(this);
}

Ext.extend(Cognitum.Polizas.General.Entity, Karma.Core.Entity, {
    id: 'Polizas.Entity',
    name: 'Polizas',
    editorXType: 'pol.editor',
    editorW: 930,
    editorH: 700,
    columns: [{
        Name: 'Poliza',
        Property: 'Folio'
    }, {
        Name: 'OT',
        Property: 'Id'
    }, {
        Name: 'Forma Pago',
        Property: 'FormaPago',
        Tipo: 'enum',
        TipoEnum: 'Cognitum.Dominio.Polizas.FormaPagoEnum'
    }, {
        Name: 'Grupo',
        Property: 'Grupo',
		EntityName: 'Grupos',
		EntityProperty: 'Nombre',
		Tipo: 'entity'
    }, {
        Name: 'Contratante',
        Property: 'Contratante',
        EntityName: 'Clientes',
        EntityProperty: 'NombreCompleto',
        Tipo: 'entity'
    }, {
        Name: 'Aseguradora',
        Property: 'Aseguradora',
		EntityName: 'Aseguradoras',
		EntityProperty: 'Clave',
		Tipo: 'entity'
    }, {
        Name: 'Ramo',
        Property: 'Ramo',
		EntityName: 'Ramos',
		EntityProperty: 'Nombre',
		Tipo: 'entity'
    }, {
        Name: 'SubRamo',
        Property: 'SubRamo',
		EntityName: 'SubRamos',
		EntityProperty: 'Nombre',
		Tipo: 'entity'
    }, {
        Name: 'Estatus',
        Property: 'EstatusPoliza',
        Tipo: 'enum',
        TipoEnum: 'Cognitum.Dominio.Polizas.EstatusPolizaEnum'
    }, {
        Name: 'Estatus Cobranza',
        Property: 'EstatusCobranza',
        Tipo: 'enum',
        TipoEnum: 'Cognitum.Dominio.Polizas.EstatusCobranzaEnum'
    }, {
        Name: 'Prima',
        Property: 'Prima',
        Mostrar: false
    }, {
        Name: 'Comision Importe',
        Property: 'ComisionImporte',
        Mostrar: false
    }, {
        Name: 'Comision Porcentaje',
        Property: 'ComisionPct',
        Mostrar: false
    }, {
        Name: 'Recargos Importe',
        Property: 'RecargosImporte',
        Mostrar: false
    }, {
        Name: 'Recargos Porcentaje',
        Property: 'RecargosPct',
        Mostrar: false
    }, {
        Name: 'Inicio Vigencia',
        Property: 'VigenciaInicio',
        Tipo: 'fecha',
		Mostrar: false
    }, {
        Name: 'Fin Vigencia',
        Property: 'VigenciaFin',
        Tipo: 'fecha',
		Mostrar: false
    }, {
        Name: 'Moneda',
        Property: 'Moneda',
		EntityName: 'Monedas',
		EntityProperty: 'Nombre',
		Tipo: 'entity'
    }, {
        Name: 'Impuesto',
        Property: 'Impuesto',
        EntityName: 'Impuestos',
        EntityProperty: 'Clave',
        Tipo: 'entity'
    }, {
        Name: 'Ejecutivo Aseguradora',
        Property: 'EjecutivoAseguradora',
		EntityName: 'Contacto Persona Moral',
		EntityProperty: 'NombreCompleto',
		Tipo: 'entity',
		Mostrar: false
    }],
    titleProperty: new Ext.XTemplate('<tpl if="!Ext.isEmpty(values.Folio)"><b>Folio : [{values.Folio}]</b></tpl>', '<tpl if="Ext.isEmpty(values.Folio)"><b>OT : [{values.Id}]</b></tpl>'),
    searchlist: {
        sortings: ['Folio'],
        views: [{
            Id: 0,
            Nombre: 'Normal',
            Template: null
        }],
        previewTemplate: null,
	    getMenuActions: function() {
	        if(!this.submenus) {
	            this.submenus = [this.openMenuAction = new Ext.menu.Item({
	                iconCls:'icon-window',
	                text:'Abrir...',
	                scope: this,
	                handler:function(){
	                    var grid = Ext.getCmp(this.getId() + '-grid');
	                    var selected = grid.getSelectionModel().getSelected();
	                    this.openRecord(selected.get('Id'));
	                }
	            }),this.deleteMenuAction = new Ext.menu.Item({
	                text:'Eliminar...',
	                iconCls:'icon-minus',
	                scope: this,
	                handler:function(){
	                    var grid = Ext.getCmp(this.getId() + '-grid');
	                    var selected = grid.getSelectionModel().getSelected();
	                    this.deleteRecord(selected.get('Id'));
	                }
	            })];
	        }
	        return this.submenus;
	    },
        getTBarCustomControls: function(){
            return ['->', ' ', {
                text: 'Nueva OT',
				iconCls: 'icon-plus',
                menu: {
                    items: [{
                        text: 'Autos',
                        menu: {
                            items: [{
                                text: 'Individual',
                                handler: function(){
                                    this.fireEvent("autoIndividual");
                                },
                                scope: this
                            
                            }, {
                                text: 'Pool',
                                handler: function(){
                                    this.fireEvent("autoPool");
                                },
                                scope: this
                            
                            }]
                        },
                        hidden: !this.security.New
                    }, {
                        text: 'Vida',
                        menu: {
                            items: [{
                                text: 'Individual',
                                handler: function(){
                                    this.fireEvent("vidaIndividual");
                                },
                                scope: this
                            
                            }, {
                                text: 'Pool',
                                handler: function(){
                                    this.fireEvent("vidaPool");
                                },
                                scope: this                            
                            }]
                        },
                        hidden: !this.security.New
                    }, {
                        text: 'GMM',
                        menu: {
                            items: [{
                                text: 'Individual',
                                handler: function(){
                                    this.fireEvent("gmmIndividual");
                                },
                                scope: this
                            }, {
                                text: 'Pool',
                                handler: function(){
                                    this.fireEvent("gmmPool");
                                },
                                scope: this
                            }]
                        },
                        hidden: !this.security.New
                    }, {
                        text: 'Daños',
                        menu: {
                            items: [{
                                text: 'Individual',
                                handler: function() {
                                    this.fireEvent("danhosIndividual");
                                },
                                scope: this
                            }, {
                                text: 'Pool',
                                handler: function() {
                                    this.fireEvent("danhosPool");
                                },
                                scope: this
                                }]
                            },
                            hidden: !this.security.New
                        }]
                    },
                hidden: !this.security.New
            }, '|', ' '];
        }
    },
    link: {
        displayProperty: new Ext.XTemplate('<tpl if="!Ext.isEmpty(values.Folio)">Folio : [{values.Folio}]</tpl>', '<tpl if="Ext.isEmpty(values.Folio)">OT : [{values.Id}]</tpl>'),
        tpl: '<tpl if="!Ext.isEmpty(values.Folio)">Folio : [{values.Folio}]</tpl><tpl if="Ext.isEmpty(values.Folio)">OT : [{values.Id}]</tpl>',
        additionalProperties: ["Aseguradora", "VigenciaInicio", "VigenciaFin"]
    },
    getCustomListeners: function(){
        return {
            'autoIndividual': {
                fn: function(options){
                    var entity = Karma.Core.ModuleManager.Instance.getEntity('AUTOIndividual');
                    entity.NewPoliza(0);
                },
                scope: this
            },
            'autoPool': {
                fn: function(options){
                    var entity = Karma.Core.ModuleManager.Instance.getEntity('AUTOPool');
                    entity.NewPoliza(0);
                },
                scope: this
            },
            'vidaIndividual': {
                fn: function(options){
                    var entity = Karma.Core.ModuleManager.Instance.getEntity('VidaIndividual');
                    entity.NewPoliza(2);
                },
                scope: this
            },
            'vidaPool': {
                fn: function(options){
                    var entity = Karma.Core.ModuleManager.Instance.getEntity('VidaPool');
                    entity.NewPoliza(2);
                },
                scope: this
            },
            'gmmIndividual': {
                fn: function(options){
                    var entity = Karma.Core.ModuleManager.Instance.getEntity('GmmIndividual');
                    entity.NewPoliza(1);
                },
                scope: this
            },
            'gmmPool': {
                fn: function(options){
                    var entity = Karma.Core.ModuleManager.Instance.getEntity('GmmPool');
                    entity.NewPoliza(1);
                },
                scope: this
            },
            'danhosIndividual': {
                fn: function(options) {
                    var entity = Karma.Core.ModuleManager.Instance.getEntity('DANHOSIndividual');
                    entity.NewPoliza(3);
                },
                scope: this
            },
            'danhosPool': {
                fn: function(options) {
                    var entity = Karma.Core.ModuleManager.Instance.getEntity('DANHOSPool');
                    entity.NewPoliza(3);
                },
                scope: this
            }
        };
    },
    
    getNewTitle: function(value){
        return 'Nueva Orden de trabajo';
    },
	
	onOpen: function(id, listeners){
        if (LOG.isDebugEnabled()) {
            LOG.debug('[Poliza.onOpen] <-');
        }

        var editorWin = Karma.WinManager.Instance.getIfExist(this, id);
        if (Ext.isEmpty(editorWin)) {            
            this.useInvoker(Karma.Conf.GetMethod, id, {
                fn: function(poliza) {
                    this.openPoliza(poliza, listeners);
                },
                scope: this
            });                        
        }
        else {           
            editorWin.show();
        }
        
        
    },
	
	openPoliza: function(poliza, listeners, editorProperties){
        if (LOG.isDebugEnabled()) {
            LOG.debug('[Poliza.onOpen2]<- poliza.Ramo=' + poliza.Ramo.Clave);
        }
        var divOperativaClave = null;
		switch (poliza.Ramo.DivisionOperativa) {
			case 0:
				divOperativaClave = 'AUTO';
				break;
			case 1:
				divOperativaClave = 'Gmm';
				break;
			case 2:
				divOperativaClave = 'Vida';
				break;
			case 3:
				divOperativaClave = 'DANHOS';
				break;
		}
		var entidad = Karma.Core.ModuleManager.Instance.getEntity(
			divOperativaClave + (poliza.SubRamo.Flotilla? 'Pool': 'Individual'));
        if (Ext.isEmpty(editorProperties)) {
			editorProperties = {};
		}
		var editorProperties = { value: poliza, listeners: listeners, isNew: false,
		 Division: poliza.Ramo.DivisionOperativa };
        entidad.openWindow(poliza, null, editorProperties, entidad.editorXType);
		this.fireEvent('open');
    },
	
	NewPoliza: function(divisionOperativa){
        this.useInvoker(Karma.Conf.NewMethod, null, {
            fn: function(poliza){
				var editorProperties = { value: poliza, isNew: true, Division: divisionOperativa };
		        this.openWindow(poliza, {}, editorProperties, this.editorXType);
				this.fireEvent('new');
            },
            scope: this
        });
    }
    
});

Cognitum.PolizaGeneral = Cognitum.Polizas.General.Entity;
